<template>
  <div class="zhenxuangu">
    <jgySdk statName="yyzs" jgyOutStyle="{}" divclass="message_padding white" :answer="answers"></jgySdk>
    <div v-if="moreInfo" class="page_more_link f14" @click="jumpMoreLink">{{ moreInfo.showText }}</div>
  </div>
</template>
<script>
export default {
  props: {
    answers: {
      type: Object,
      default: () => {}
    },
    intent: {
      type: String,
      default: ""
    }
  },
  watch: {
    answers: {
      handler(newVal) {
        console.log("zhengxuangu answers changed", newVal);
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {};
  },
  created() {
    console.log("zhengxuangu created");
  },
  components: {},
  computed: {
    moreInfo() {
      let result = null;
      const zhenguData = this.answers;
      if (
        zhenguData &&
        zhenguData.page &&
        zhenguData.components.length > 0 &&
        !this.lastComponentIsTable1(
          zhenguData.components[zhenguData.components.length - 1]
        )
      ) {
        let page = zhenguData.page;
        if (page.more && page.more.isview && page.more.q) {
          result = {
            query: page.more.q,
            showText: "查看更多",
            queryType: page.more.query_type
          };
        }
        if (!page.more.isview) {
          zhenguData.components.forEach(s => {
            if (
              s.config &&
              s.config.other_info &&
              s.config.other_info.footer_info &&
              "iwencai" === this.intent
            ) {
              if (s.show_type === "tableV1") {
                try {
                  const row_count = s.data.meta.extra.row_count || 0;
                  const url = s.config.other_info.footer_info.url;
                  const perpage = url.match(/&perpage=\d+/)[0].slice(9) || 0;
                  if (row_count > perpage * 1 && s.config.showLoadMore) {
                    result = {
                      query: page.more.q,
                      showText: "查看全部",
                      queryType: page.more.query_type
                    };
                  }
                } catch (e) {
                  console.log("数据解析异常", e);
                }
              } else {
                result = {
                  query: page.more.q,
                  showText: "查看全部",
                  queryType: page.more.query_type
                };
              }
            }
          });
        }
      }
      return result;
    }
  },
  methods: {
    //跳转更多
    jumpMoreLink: function() {
      window.zhenguHref.iwencaiPage(
        this.moreInfo.query,
        this.moreInfo.queryType
      );
    },
    lastComponentIsTable1: function(e) {
      console.log("----lastComponentIsTable1-------e", e);
      return e && "table1" === e.show_type;
    }
  }
};
</script>
<style lang="less" scoped>
@import (once) "@/styles/variable.less";
.page_more_link {
  height: @height25;
  line-height: @height20;
  text-align: center;
  color: #2f86f6;
  font-size: 0.56rem;
  padding: 0.2rem 0;
}
</style>
